% File: specification-VAR-Matl.txt (Example 11.5)
%
% Related to TT.dat (tree data); estimates of AR are from Harvill-Ray Fortran code
% H & R fortran (applytot.f) completely agree with R-VAR estimation results, see below.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code:
nr= 66;               
v111=0.3117; v211= 0.5217;  v121= 0.0199; v221= 0.3225; % 1st series, 2nd series, 3rd lag
v122=0.4159; v212= 0.7665;  v112= 0.0268; v222=-0.0284;
v113=0.0743; v213= 0.1105;  v123=-0.0234; v223= 0.1764;
v114=0.1549; v214=-0.9208;  v124= 0.0173; v224= 0.0830;
for i=5:nr
   yfit(i,1)= 0.31169777*TT(i-1,1)+ 0.01985487*TT(i-1,2)+... 
              0.41587592*TT(i-2,1)+ 0.02675673*TT(i-2,2)+...  
              0.07434659*TT(i-3,1) -0.02337063*TT(i-3,2)+...  
              0.15493760*TT(i-4,1)+ 0.01733847*TT(i-4,2); 

   yfit(i,2)= 0.52171206*TT(i-1,1)+ 0.32251735*TT(i-1,2)+...
              0.76645106*TT(i-2,1)- 0.02841470*TT(i-2,2)+...
              0.11046843*TT(i-3,1)+ 0.17638240*TT(i-3,2)+...
             -0.92083215*TT(i-4,1)+ 0.08302244*TT(i-4,2); 
   
   res(i,1)=TT(i,1)-yfit(i,1);
   res(i,2)=TT(i,2)-yfit(i,2);
end
v = diag(cov(r))'

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% R code

Totvar <-VAR(TT,p=4,type="none")

VAR Estimation Results  from R:
======================= 
Estimated coefficients for equation V1: 
======================================= 
Call:
V1 = V1.l1 + V2.l1 + V1.l2 + V2.l2 + V1.l3 + V2.l3 + V1.l4 + V2.l4 
      V1.l1       V2.l1       V1.l2       V2.l2       V1.l3       V2.l3       V1.l4       V2.l4 
 0.31169777  0.01985487  0.41587592  0.02675673  0.07434659 -0.02337063  0.15493760  0.01733847 

Estimated coefficients for equation V2: 
======================================= 
V2 = V1.l1 + V2.l1 + V1.l2 + V2.l2 + V1.l3 + V2.l3 + V1.l4 + V2.l4 
      V1.l1       V2.l1       V1.l2       V2.l2       V1.l3       V2.l3       V1.l4       V2.l4 
 0.52171206  0.32251735  0.76645106 -0.02841470  0.11046843  0.17638240 -0.92083215  0.08302244 

resid1 <- Totvar$varresult$V1$residuals
resid2 <- Totvar$varresult$V2$residuals

write(Xres1,"g:\\Xmat1.dat",ncol=1)
write(Xres2,"g:\\Xmat2.dat",ncol=1)


>> Xmattot= [Xmat1, Xmat2];
>> Xmattot'* Xmattot

ans =

  1.0e+003 *

    0.1101   -0.1558
   -0.1558    5.0657

 SSE from Linear Model
       110.1074      -155.7958
      -155.7958      5065.7221
